package android.crosstime.hes;

import java.io.File;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import edu.android.hes.R;

public class FileListAdapter extends BaseFileListAdapter {

	RelativeLayout mActLayout = null;
	
	public FileListAdapter(Context context){
		super(context);
	}
	
	public FileListAdapter(Context context, RelativeLayout dialogLayout){
		super(context);
		mActLayout = dialogLayout;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null)
		{
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.img_view);
			holder.text = (TextView) convertView.findViewById(R.id.text_view);
			holder.listBtn = (ImageView) convertView.findViewById(R.id.img_right);			
			convertView.setTag(holder);
		}
		else
			holder = (ViewHolder) convertView.getTag();
		ListItem li = mFileList.get(position);
		String text = li.Name;
		holder.text.setText(text);
		int icon = li.IconId;
		holder.icon.setImageResource(icon);
		if(li.isDirectory() && ((new File(li.FullPath)).listFiles() != null) 
				&& !li.Name.equals(".."))
		{
//			holder.listBtn.setImageResource(R.drawable.ic_aggregated);
			holder.listBtn.setOnClickListener(new OnDialogClickListener(li.FullPath));
			holder.listBtn.setVisibility(View.VISIBLE);
		}
		else
			holder.listBtn.setVisibility(View.INVISIBLE);
		return convertView;
	}

	private class OnDialogClickListener implements OnClickListener {

		public String filePath = null;
		
		public OnDialogClickListener(String path) {
			this.filePath = path;
		}
		
		@Override
		public void onClick(View v) {
			FloatDialog view = new FloatDialog(mContext, filePath);
			RelativeLayout.LayoutParams ly = new RelativeLayout.LayoutParams(300, 300);
			ly.leftMargin = 60;
			ly.topMargin = 100;
			mActLayout.addView(view, ly);
		}
		
	}
	
}
